﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using TheOliver.DigitalSignage.Admin.Controls;
using Web.Services;

namespace TheOliver.DigitalSignage.Admin.Views
{
    public partial class Speaker : Page
    {
        public Speaker()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void speakerDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void _add_Click(object sender, RoutedEventArgs e)
        {
            DigitalSignageDomainContext context = 
                speakerDomainDataSource.DomainContext as DigitalSignageDomainContext;
            
            Web.Speaker speaker = new Web.Speaker();
            speaker.Lastname = "Lastname";
            speaker.Firstname = "Firstname";
            speaker.Title = "Speaker";
            speaker.Biography = "Speaker Bio";
            speaker.ImageUrl = "";

            context.Speakers.Add(speaker);
            context.SubmitChanges();
        }

        private void _delete_Click(object sender, RoutedEventArgs e)
        {
            if (speakerDataGrid.SelectedItem != null)
            {
                QuestionWindow qw = 
                    new QuestionWindow(TextResource.DeleteSpeakerQuestion, DeleteSpeaker);
                qw.Show();
            }
        }

        private void _save_Click(object sender, RoutedEventArgs e)
        {
            speakerDomainDataSource.SubmitChanges();
        }

        private void _reject_Click(object sender, RoutedEventArgs e)
        {
            speakerDomainDataSource.RejectChanges();
        }

        private void DeleteSpeaker()
        {
            DigitalSignageDomainContext context = 
                speakerDomainDataSource.DomainContext as DigitalSignageDomainContext;

            Web.Speaker speaker = speakerDataGrid.SelectedItem as Web.Speaker;
            context.Speakers.Remove(speaker);
        }
    }
}
